package com.shundr.shipper.cargo;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.shundr.shipper.R;
import com.shundr.shipper.base.BaseActivity;
import com.shundr.shipper.cargo.http.CargoRequest;
import com.shundr.shipper.cargo.model.CargoInfo;
import com.shundr.shipper.common.http.CommonRequest;
import com.shundr.shipper.common.util.CommonConfig;
import com.shundr.shipper.common.util.DateUtil;
import com.shundr.shipper.common.util.ProgressDialogHelper;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.frame.constant.LContext;
import com.shundr.shipper.frame.http.LHttpRequest;
import com.shundr.shipper.frame.service.PhoneReceiver;
import com.shundr.shipper.frame.service.PhoneReceiver.PhoneHandler;
import com.shundr.shipper.frame.util.StringUtil;
import com.shundr.shipper.truck.ContactTrucker;
import com.shundr.shipper.truck.ContactTrucker.OnCallListener;
import com.shundr.shipper.user.LoginActivity;

/**
 * 货源详情
 * CargoDetailsActivity
 * 
 * @author larry 2015-5-20 下午2:52:22 
 * @version 1.0.0
 *
 */
public class CargoDetailsActivity extends BaseActivity implements OnClickListener,PhoneHandler {
	/**
	 * 货物Id
	 */
	private String mCargoId = "";
	/**
	 * 始发地/目的地
	 */
	private TextView tvSrcPlace, tvDestPlace;
	/**
	 * 货物信息
	 */
	private TextView tvCargoName;
	/**
	 * 车辆需求
	 */
	private TextView tvTruckRequire;
	/**
	 * 信息来源
	 */
	private TextView tvInfoFrom;
	/**
	 * 货物价格
	 */
	private TextView tvCargoPrice;
	/**
	 * 联系方式
	 */
	private TextView tvContact;
	/**
	 * 发布时间
	 */
	private TextView tvInTime;
	
	private TextView tvExpiry;
	
	private TextView tvRemark;
	private TextView tvStatus;

	/**
	 * 拨打电话
	 */
	private Button btnCallPhone;
	private Button btnFavorite;

	private CargoInfo mInfo;
	
	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case CargoRequest.GET_CARGO_INFO_SUCCESSED:
				try {
					mInfo = (CargoInfo) msg.obj;
					showCargoDetails(mInfo);
				} catch (Exception e) {
					e.printStackTrace();
				}
				ProgressDialogHelper.dismiss();
				break;
			case CargoRequest.REQUEST_FAILED:
				try {
					ProgressDialogHelper.dismiss();
					ToastHelper.toastShort(getApplicationContext(),msg.obj.toString());
					finish();
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cargo_detail);
		tvSrcPlace = (TextView) findViewById(R.id.cargo_details_src);
		tvDestPlace = (TextView) findViewById(R.id.cargo_details_dst);
		tvCargoName = (TextView) findViewById(R.id.cargo_details_cargoinfo);
		tvTruckRequire = (TextView) findViewById(R.id.cargo_details_truck_require);
		tvInfoFrom = (TextView) findViewById(R.id.cargo_details_information_from);
		tvCargoPrice = (TextView) findViewById(R.id.cargo_details_price);
		tvContact = (TextView) findViewById(R.id.cargo_details_contact);
		tvInTime = (TextView) findViewById(R.id.cargo_details_in_time);
		tvExpiry = (TextView) findViewById(R.id.cargo_details_expiry_time);
		tvRemark = (TextView) findViewById(R.id.tv_remark);
		tvStatus = (TextView) findViewById(R.id.tv_status);
		
		btnCallPhone = (Button) findViewById(R.id.cargo_details_call_phone);
		btnFavorite = (Button) findViewById(R.id.btn_favorite);
		btnCallPhone.setOnClickListener(this);
		btnFavorite.setOnClickListener(this);
		mCargoId = getIntent().getStringExtra("CargoId");
		init();
		PhoneReceiver.mPhoneListeners.add(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cargo_details_call_phone:
			if (LContext.isLogin()) {
				try {
					new CommonRequest(mContext, new Handler()).saveOperateHistory(mInfo.getCargoId(), CommonConfig.OPERATE_OBJECT_CARGO, CommonConfig.OPERATE_CALL,"");
					if (LContext.getUserValidationStatus() != -1) {//认证完
						OnCallPhone(mInfo);
					}else{//未认证通过
						ContactTrucker trucker = new ContactTrucker(mContext);
						trucker.setCallListener(new OnCallListener() {
							
							@Override
							public void onCall() {
								OnCallPhone(mInfo);
							}
						});
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				mContext.startActivity(new Intent(mContext, LoginActivity.class));
			}
			break;
		case R.id.btn_favorite:
			new CommonRequest(mContext, new Handler(){

				@Override
				public void handleMessage(Message msg) {
					super.handleMessage(msg);
					switch (msg.what) {
					case CommonRequest.ADD_FAVORITE_SUCCEED:
						try {
							ToastHelper.toastShort(mContext,msg.obj.toString());
							btnFavorite.setText("已收藏");
							btnFavorite.setBackgroundResource(R.drawable.item_common_gray_selector);
							btnFavorite.setClickable(false);
						} catch (Exception e) {
							e.printStackTrace();
						}
						break;
					case LHttpRequest.REQUEST_FAILED:
						try {
							ToastHelper.toastShort(mContext,msg.obj.toString());
						} catch (Exception e) {
							e.printStackTrace();
						}
						break;
					default:
						break;
					}
				}
				
			}).addFavorite(Integer.parseInt(mCargoId), CommonConfig.OPERATE_OBJECT_CARGO);
			break;
		default:
			break;
		}
	}

	private void OnCallPhone(CargoInfo info) {
		try {
			if (!StringUtil.isNull(info.getCargoCompanyPhone1())) {
				String mobileText = info.getCargoCompanyPhone1().replace("-", "");
				Intent intent = new Intent();// 创建一个意图对象，用来激发拨号的Activity
				intent.setAction("android.intent.action.CALL");
				intent.setData(Uri.parse("tel:" + mobileText));
				mContext.startActivity(intent);// 方法内部会自动添加类别,android.intent.category.DEFAULT
			} else {
				ToastHelper.toastShort(mContext, "该货主未提供联系电话");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void init() {
		try {
			if (!StringUtil.isNull(mCargoId))
				mCargoId = getIntent().getStringExtra("CargoId");
			ProgressDialogHelper.show(mContext, "正在加载中...");
			new CargoRequest(CargoDetailsActivity.this, mHandler).getCargoInfo(mCargoId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void showCargoDetails(CargoInfo info) {
		try {
			if (!StringUtil.isNull(info.getCargoSrcPlace())) {
				tvSrcPlace.setText(info.getCargoSrcPlace());
			}
			if (!StringUtil.isNull(info.getCargoDstPlace())) {
				tvDestPlace.setText(info.getCargoDstPlace());
			}
			String cargoCategory = "";
			String cargoAmount = "";
			if (!StringUtil.isNull(info.getCargoCategory())) {
				cargoCategory = info.getCargoCategory() + "  ";
			}
			if (!StringUtil.isNull(info.getCargoAmount() + "")) {
				cargoAmount = info.getCargoAmount() + info.getCargoUnitName();
			}
			tvCargoName.setText(cargoCategory + cargoAmount);
			String truckType = "";
			String truckLength = "";
			String truckNum = "";
			if (!StringUtil.isNull(info.getCargoTruckType())) {
				truckType = info.getCargoTruckType() + "  ";
			}
			if (!StringUtil.isNull(info.getCargoTruckLength() + "")) {
				if(StringUtil.isEqual(info.getCargoTruckLength(), "23")){
					truckLength = info.getCargoTruckLength() + "米以上" + "  ";
				}else{
					truckLength = info.getCargoTruckLength() + "米" + "  ";
				}
			}
			if (!StringUtil.isNull(info.getCargoTruckNum() + "")) {
				truckNum = info.getCargoTruckNum() + "辆";
			}
			String require = truckType + truckLength + truckNum;
			if(StringUtil.isNull(require)){
				tvTruckRequire.setText("不限");
			}else{
				tvTruckRequire.setText(require);
			}
			if(!StringUtil.isNull(info.getCargoContactPerson())){
				tvInfoFrom.setText(info.getCargoContactPerson());
			}else if (!StringUtil.isNull(info.getCargoCompanyName())) {
				tvInfoFrom.setText(info.getCargoCompanyName());
			}else{
				tvInfoFrom.setText("未知");
			}
			if (!StringUtil.isNull(info.getCargoUnitPrice() + "")) {
				tvCargoPrice.setText(info.getCargoUnitPrice() + " 元/ " + info.getCargoUnitName());
			}else{
				tvCargoPrice.setText("面谈");
			}
			if (!StringUtil.isNull(info.getCargoCompanyPhone1())) {
				String phone=info.getCargoCompanyPhone1();
				tvContact.setText(phone);
			}
			if (!StringUtil.isNull(info.getCargoCompanyPhone1())) {
				String phone=info.getCargoCompanyPhone1();
				if(LContext.getUserValidationStatus() != -1){
					tvContact.setText(phone);
				}else{
					phone=phone.substring(0, phone.length()-4)+"****";
					tvContact.setText(phone);
				}
			}
			if (!StringUtil.isNull(info.getCargoResendTime())) {//getCargoInsertTime
				tvInTime.setText(info.getCargoResendTime());
			}else{
				tvInTime.setText("不详");
			}
			if (!StringUtil.isNull(info.getCargoExpiryTime())) {
				tvExpiry.setText(DateUtil.getDate_1(info.getCargoExpiryTime()));
			}else{
				tvExpiry.setText("不限");
			}
			if (!StringUtil.isNull(info.getCargoRemarks())) {
				tvRemark.setText(info.getCargoRemarks());
			}else{
				tvRemark.setText("无");
			}
			if (StringUtil.isEqual(info.getCargoStatus() + "", "1")) {
				tvStatus.setText("待成交");
			} else {
				tvStatus.setText("已下架");
				tvStatus.setTextColor(getResources().getColor(R.color.btn_orange_light));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		mHandler.removeCallbacksAndMessages(null);
		PhoneReceiver.mPhoneListeners.remove(this);
		super.onDestroy();
	}

	@Override
	public void onPhoned() {
	}

}
